package com.yootk.context.config;

import com.yootk.config.mapper.CustomObjectMapper;
import com.yootk.interceptor.YootkHandlerInterceptor;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.util.UrlPathHelper;

import java.util.List;

@Configuration
@EnableWebMvc
@ComponentScan("com.yootk.action")
public class SpringWebConfig implements WebMvcConfigurer {
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        var urlPathHelper = new UrlPathHelper() ;
        urlPathHelper.setRemoveSemicolonContent(false);
        configurer.setUrlPathHelper(urlPathHelper) ;
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter() ;
        CustomObjectMapper mapper = new CustomObjectMapper() ;
        converter.setObjectMapper(mapper);
        converters.add(converter) ;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("yootk-js/**").addResourceLocations("/WEB-INF/static/js/") ;
        registry.addResourceHandler("yootk-css/**").addResourceLocations("/WEB-INF/static/css/") ;
        registry.addResourceHandler("yootk-images/**").addResourceLocations("/WEB-INF/static/images/") ;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new YootkHandlerInterceptor())
                .addPathPatterns("/pages/**") ;
    }
}
